//
//  NoteDraftMode.m
//  DogeLegs
//
//  Created by 宋华 on 2017/8/20.
//  Copyright © 2017年 宋华. All rights reserved.
//

#import "NoteDraftMode.h"
#import <objc/runtime.h>

@implementation NoteDraftMode


/*
 使用runtime进行解档与归档。
 */
-(void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar *ivarLists = class_copyIvarList([NoteDraftMode class], &count);// 注意下面分析
    for (int i = 0; i < count; i++) {
        const char* name = ivar_getName(ivarLists[i]);
        NSString* strName = [NSString stringWithUTF8String:name];
        [aCoder encodeObject:[self valueForKey:strName] forKey:strName];
    }
    free(ivarLists);   //一定不要忘了，自己释放。
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        unsigned int count = 0;
        Ivar *ivarLists = class_copyIvarList([NoteDraftMode class], &count);
        for (int i = 0; i < count; i++) {
            const char* name = ivar_getName(ivarLists[i]);
            NSString* strName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            id value = [aDecoder decodeObjectForKey:strName];
            [self setValue:value forKey:strName];
        }
        free(ivarLists);
    }
    return self;
}

+ (NoteDraftMode *)saveDraftMode:(NSString *)price noteTitle:(NSString *)noteTitle noteContent:(NSString *)content noteCateory:(NoteCateoryMode *)nocat disImgs:(NSMutableArray *)disImgs uplImgs:(NSMutableArray *)uplImgs {
    NoteDraftMode *draftMode = [[NoteDraftMode alloc] init];
    draftMode.price = price;
    draftMode.noteTitle = noteTitle;
    draftMode.noteContent = content;
    draftMode.notecateory = nocat;
    draftMode.displayImages = disImgs;
    draftMode.uploadImages = uplImgs;
    return draftMode;
}


@end
